/*
** File name:		window_menu_user_add_auth.c
** Brief: 
** COPYRIGHT(C) gkt 2021 
** ------------------------------------------------------- 
** Module Information: 
** 
** Revision History -------------------------------------- 
** ------------------------------------------------------- 
**	Date 		Version		Action				Author   
** ------------------------------------------------------- 
**	2021-1-12 	1.0			Created				liwenjie   
** 
*/ 

/* Includes -----------------------------------------------------*/
#include "gkt_types.h"
#include "gkt_os.h"
#include "gkt_system.h"
#include "gkt_debug.h"
#include "gkt_user.h"
#include "window_common.h"
#include "window_menu_user_add.h"

/* Global Variables ---------------------------------------------*/
/* Macro --------------------------------------------------------*/
enum {
	WINDOW_ID_USER_ADD_AUTH_TITLE = 0,	
	WINDOW_ID_USER_ADD_AUTH_LIST,
	WIDGET_ID_MAX
};

static const gkt_widget_list_item_s sc_window_user_add_auth_widget_list_items[] = {
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_admin_user, GKT_USER_AUTH_ADMIN),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_normal_user, GKT_USER_AUTH_NORMAL),
#if (GKT_USER_GUEST_MAXNUMS > 0)
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_guest_user, GKT_USER_AUTH_GUEST),
#endif
#if defined(GKT_CONFIG_USER_TYPE_THREATEN_ENABLE) && GKT_CONFIG_USER_TYPE_THREATEN_ENABLE
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_threaten_user, GKT_USER_AUTH_THREATEN),
#endif

};

static const gkt_widget_list_attr_s sc_window_user_add_auth_widget_list_attr = 
	GKT_WIDGET_LIST_STATIC_ATTR(WINDOW_WIDGET_NORMAL_HEIGHT, 
		sizeof(sc_window_user_add_auth_widget_list_items) / sizeof(gkt_widget_list_item_s),
		sc_window_user_add_auth_widget_list_items);

GKT_WINDOW_WIDGET_DESCS_DEFINE(user_add_auth) = {
	[WINDOW_ID_USER_ADD_AUTH_TITLE] = GKT_WIDGET_LABEL_DESC(
			GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
			WINDOW_FONT_NORMAL_SIZE, 
			WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
			0, 0, 
			WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
			WINDOW_TEXT_ID_add_user),
	[WINDOW_ID_USER_ADD_AUTH_LIST] = GKT_WIDGET_LIST_DESC(
			GKT_WIDGET_F_FOCUSABLE | GKT_WIDGET_F_ALIGN_MIDDLE,
			WINDOW_FONT_NORMAL_SIZE, WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR, 
			0, WINDOW_WIDGET_NORMAL_HEIGHT, 
			WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT * (WINDOW_NORMAL_ROWS - 1),
			&sc_window_user_add_auth_widget_list_attr),
};

typedef struct _window_user_add_auth {
	window_user_add_data_s 	*add_data_ptr;
	window_user_add_data_s	add_data;
} window_user_add_auth_s;
static window_user_add_auth_s s_user_add_auth;

/* Extern Functions ---------------------------------------------*/
/* Static Functions ---------------------------------------------*/
static int window_user_add_auth_full_handler(void)
{
	gkt_user_amount_s amount;
	int retval;
	retval = gkt_user_get_amount(&amount, s_user_add_auth.add_data_ptr->authority,
		GKT_USER_TYPE_MAX);
	if ((GKT_SUCCESS == retval)
		&& (amount.used_nums >= amount.max_nums)) 
	{
		window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_FAILED,
				gc_window_add_auth_user_text_ids[s_user_add_auth.add_data_ptr->authority],
				WINDOW_TEXT_ID_user_full,
				WINDOW_VOICE_COMBINED_ID(user_already_full));

		return GKT_EVENT_HANDLE_COMPLETE;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}

static void window_user_add_auth_init_data(void)
{
	s_user_add_auth.add_data_ptr = &s_user_add_auth.add_data;
	s_user_add_auth.add_data_ptr->authority = GKT_USER_AUTH_MAX;
	s_user_add_auth.add_data_ptr->threaten = 0;
	s_user_add_auth.add_data_ptr->type = GKT_USER_TYPE_MAX;
	s_user_add_auth.add_data_ptr->user_index = -1;
}

static int window_user_add_auth_event_handler(
					uint32_t major_type, uint32_t minor_type, 
					uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	if (GKT_EVENT_WINDOW == major_type) {
		if (GKT_WINDOW_INIT == minor_type) {
			s_user_add_auth.add_data_ptr = (window_user_add_data_s *)data;
			if (!s_user_add_auth.add_data_ptr)
				window_user_add_auth_init_data();
		}
		else if (GKT_WINDOW_ACTIVE == minor_type) {
			if (data) {	/* if 1, first active */
				if (s_user_add_auth.add_data_ptr->authority <= GKT_USER_AUTH_GUEST) {
					retval = window_user_add_auth_full_handler();
					if (GKT_EVENT_HANDLE_CONTINUE == retval) {
						gkt_window_show(WINDOW_ID_user_add_type, 
								(uint32_t)s_user_add_auth.add_data_ptr);
					}
					else
						gkt_window_exit();
					return GKT_EVENT_HANDLE_COMPLETE;
				}
			}
			else {
				gkt_user_amount_s amount;

				amount.used_nums = 0;
				gkt_user_get_amount(&amount, GKT_USER_AUTH_ADMIN, GKT_USER_TYPE_MAX);
				if (!amount.used_nums) {
					gkt_window_exit();
					return GKT_EVENT_HANDLE_COMPLETE;
				}

				window_user_add_auth_init_data();
			}

			window_common_mask_except_updn_cancel_ok();
			//gkt_window_set_voice(WINDOW_VOICE_COMBINED_ID(please_select_user_authority),
				//	GKT_WINDOW_VOICE_F_FOLLOWING | GKT_WINDOW_VOICE_F_NOTIFY_END 
					//| GKT_WINDOW_VOICE_F_NOTIFY_ERROR);
		}
	}	
	else if (GKT_EVENT_WIDGET == major_type) {
		if (GKT_WIDGET_EVENT_CLICK == minor_type) {
			s_user_add_auth.add_data_ptr->authority = 
						sc_window_user_add_auth_widget_list_items[data].data;;
			s_user_add_auth.add_data_ptr->threaten = 0;
#if defined(GKT_CONFIG_USER_TYPE_THREATEN_ENABLE) && GKT_CONFIG_USER_TYPE_THREATEN_ENABLE
			if (GKT_USER_AUTH_THREATEN == s_user_add_auth.add_data_ptr->authority)
				s_user_add_auth.add_data_ptr->threaten = 1;
#endif
			retval = window_user_add_auth_full_handler();
			if (GKT_EVENT_HANDLE_CONTINUE == retval) {
				gkt_window_show(WINDOW_ID_user_add_type, (uint32_t)s_user_add_auth.add_data_ptr);
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
		}
	}
	
	return retval;
}

/* Functions ----------------------------------------------------*/
GKT_WINDOW_DESC_DEFINE(user_add_auth); 
/* end */

